#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: $0 <command> <loop_count>"
    echo "Example: $0 'ls -l' 5"
    exit 1
fi

if ! [[ "$2" =~ ^[1-9][0-9]*$ ]]; then
    echo "Error: Loop count must be a positive integer."
    exit 1
fi

command_to_execute="$1"
loop_count="$2"

echo "Executing command ($command_to_execute) $loop_count times."

for ((i = 1; i <= loop_count; i++)); do
    echo -e "\n======================> Executing iteration $i of command..."
    
    if ! eval "$command_to_execute"; then
        echo "ERROR: Iteration $i failed with exit code $?" >&2
        exit 1
    fi
done

echo "Successfully completed all $loop_count iterations."
